package com.cj.web.back;

import com.cj.entity.Course;
import com.cj.entity.PageBean;
import com.cj.service.CourseService;
import com.cj.utils.FileNameUtils;
import com.cj.utils.JSONUtils;
import com.cj.utils.ResultVO;
import com.cj.utils.UploadUtils;
import com.cj.web.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@MultipartConfig
@WebServlet("/course")
public class CourseServlet extends BaseServlet {
    ResultVO vo = null;
    CourseService service = new CourseService();

//    上传图片和视频
    public void upload(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//        把表单的name值传过去   .. 返回一个图片的名字  xxx.jpg
        String fileName = UploadUtils.upload("fileName", req);
        System.out.println(fileName);
//        new ResultVO(200,"")

        if (fileName.endsWith(".png") || fileName.endsWith(".jpg") || fileName.endsWith(".jpeg") || fileName.endsWith(".gif")){
            System.out.println(111);
            vo = new ResultVO(200,"图片上传成功",fileName);
        }else {
             vo = new ResultVO(200, "视频上传成功", fileName);
        }
        JSONUtils.objToJson(vo,resp);
    }


//    删除已上传的文件
    public void deleteFile(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String deleteUrl = req.getParameter("deleteUrl");
        String fileName = FileNameUtils.substringFileName(deleteUrl);
        String filePath = "C:\\Users\\Administrator\\Desktop\\img\\"+fileName;
        File file = new File(filePath);
        System.out.println(file);
        System.out.println(file.delete());

        if (fileName.endsWith(".png") || fileName.endsWith(".jpg") || fileName.endsWith(".jpeg") || fileName.endsWith(".gif")){
            vo = new ResultVO(200,"图片删除成功",null);
        }else {
            vo = new ResultVO(200, "视频删除成功", null);
        }
        JSONUtils.objToJson(vo,resp);
    }



//    新增课程
    public void insertCourse(HttpServletRequest req,HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException {

//
        Map<String, String[]> map = req.getParameterMap();
        Course course = new Course();
//        把 map的值赋值给  Course对象    因为Course里面的变量，和传入参数变量名字不匹配，不会进行赋值
        BeanUtils.populate(course,map);

//        需要对 img 和 video 单独赋值   courseImage    courseVideo.. 将url变为文件名
        String imgUrl = req.getParameter("dialogImageUrl");
        String imgName = FileNameUtils.substringFileName(imgUrl);
        course.setCourseImage(imgName);

        String videoUrl = req.getParameter("dialogVideoUrl");
        String videoName = FileNameUtils.substringFileName(videoUrl);
        course.setCourseVideo(videoName);

//        封装完Course调用service，
        int row = service.insertCourse(course);

        if (row >0){
         vo = new ResultVO(200,"添加成功",null);
        }else{
            vo= new ResultVO(500,"添加失败",null);
        }
        JSONUtils.objToJson(vo,resp);

    }

//    查找课程页面，带参数 currentPage  pageSize search
    public void findPages(HttpServletRequest req,HttpServletResponse resp) throws IOException {
//        创建一个页面对象，把对象数据返还回去。。包括 totalCount总条数，你传入的参数，数据
//        页面对象用泛型写，其他页面也可能调用这个页面对象，返回不同Object的页面对象
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        String search = req.getParameter("search");


//        调用业务，封装一个PageBean对象，返回数据
        PageBean<Course> pb = service.findPage(currentPage,pageSize,search);

//        直接返回pb
        JSONUtils.objToJson(pb,resp);
    }

// 修改课程
public void updateCourse(HttpServletRequest req,HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException {
    Map<String, String[]> map = req.getParameterMap();
    Course course = new Course();
    BeanUtils.populate(course,map);
    String imgUrl = FileNameUtils.substringFileName(req.getParameter("dialogImageUrl"));
    String videoUrl = FileNameUtils.substringFileName(req.getParameter("dialogVideoUrl"));
    course.setCourseImage(imgUrl);
    course.setCourseVideo(videoUrl);

//    调用service 更新课程
    int row = service.updateCourse(course);
    if (row >0){
       vo = new ResultVO(200,"更新成功",null);
    }else{
        vo = new ResultVO(500,"更新失败",null);
    }

    JSONUtils.objToJson(vo,resp);
}


public void deleteAll(HttpServletRequest req,HttpServletResponse resp) throws IOException {
    String ids = req.getParameter("ids");
    int row = service.deleteByCids(ids);

    if(row>0){
        vo = new ResultVO(200,"删除成功",null);
    }else{
        vo = new ResultVO(500,"删除失败",null);
    }
    JSONUtils.objToJson(vo,resp);
}

public void findAllCourse(HttpServletRequest req,HttpServletResponse resp) throws IOException {
    String uid = req.getParameter("uid");
    List<Course> list = service.findAllCourse(uid);

       JSONUtils.objToJson(list,resp);
}

}
